package net.androy.smugmugtool;

import net.androy.smugmugtool.smugmugapi.SmugmuAPIManager;
import net.androy.smugmugtool.vo.LoginInfo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
	setContentView(R.layout.login);

	final Button loginBtn = (Button) findViewById(R.id.loginButton);
	final EditText username = (EditText) findViewById(R.id.username);
	final EditText password = (EditText) findViewById(R.id.password);

	final Handler handler = new Handler();

	loginBtn.setOnClickListener(new Button.OnClickListener() {
	    public void onClick(View v) {
		loginBtn.setEnabled(false);
		username.setEnabled(false);
		password.setEnabled(false);

		setProgressBarIndeterminateVisibility(true);
		Log.d("Login/username", username.getText().toString());
		Log.d("Login/password", password.getText().toString());
		Runnable r = new Runnable() {
		    public void run() {

			LoginInfo info = SmugmuAPIManager.getInstance().login(
				username.getText().toString().trim(),
				password.getText().toString().trim());

			if (info != null && info.getSession() != null) {
			    Log.d("Session ID from Smugmug:::: ", info
				    .getSession());
			    Runnable r = new Runnable() {
				public void run() {
				    setProgressBarIndeterminateVisibility(false);
				    startActivity(new Intent(
					    "net.androy.smugmugtool.SEARCH_VIEW"));
				}
			    };

			    handler.post(r);
			} else {
			    Runnable r = new Runnable() {
				public void run() {
				    setProgressBarIndeterminateVisibility(false);
				    loginBtn.setEnabled(true);
				    username.setEnabled(true);
				    password.setEnabled(true);
				}
			    };

			    handler.post(r);
			}
		    }
		};

		new Thread(r).start();

	    }
	});

    }
}
